package com.ruoyi.web.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BUser;
import com.ruoyi.system.domain.request.CaseRequest;
import com.ruoyi.system.domain.vo.BCaseVo;
import com.ruoyi.system.domain.vo.BUserVo;
import com.ruoyi.system.service.BCaseService;
import com.ruoyi.system.service.BUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    BUserService userService;

    @Autowired
    BCaseService caseService;

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo(){
        AjaxResult ajax = AjaxResult.success();
        BUser user = userService.getById(SecurityUtils.getUserId());
        if(ObjectUtils.isNotEmpty(user)){
            BUserVo userVo = new BUserVo();
            BeanUtils.copyProperties(user, userVo);
            Integer totalScores = userService.getTotalScores(user.getId());
            userVo.setTotalScores(totalScores);
            ajax.put("user", userVo);
        }

        return ajax;
    }


    /**
     * 分页获取我的案例列表
     */
    @GetMapping("/caseList")
    public TableDataInfo caseList(CaseRequest request){
        request.setUserId(SecurityUtils.getUserId());
        startPage();
        List<BCaseVo> list = caseService.queryList(request);
        return getDataTable(list);
    }

    /**
     * 修改用户信息
     * @param buser
     * @return
     */
    @PutMapping("/edit")
    public AjaxResult editUser(@Validated @RequestBody BUser buser){
        return toAjax(userService.updateById(buser));
    }

}
